#ifndef API_H
#define API_H

#include <QObject>
#include <QNetworkReply>
#include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <iostream>
#include "json/json.h"
#include <fstream>
#include <QJsonArray>
#include "dishrecommend.h"
#include "food.h"
#include "storage.h"
#include "remind.h"
#include "fridge.h"
#include "sql.h"
#include "unit.h"
#include "foodtype.h"
class Api : public QObject
{
    Q_OBJECT
public:
    explicit Api(QObject *parent = nullptr);
    void loginByPassword(QString phone,QString password);
    void getUserInfo();
    void getRecommendDish();
    void searchDish(QString keyword,int size,int index);
    void infoCer();
    void coverFromServer();
    void coverByTerminal();
    void isDirty();
    void synchronize();
    void downloadCoreData();
    void processFailed(QNetworkReply *reply);
    void bind(QString password);

private:
    int successCode = 0;
    int tokenTimeoutCode = 23000;
    QString protocol = "http://";
    QString domain = "fridge.binw.top";
//    QString domain = "localhost";
    QString port = ":8082";
    // http://fridge.binw.top:8082/user/auth/loginByPassword
    QString loginByPasswordUrl = protocol + domain + port + "/user/auth/loginByPassword";
    QString userInfoUrl = protocol + domain + port + "/user/auth/info";
    QString getRecommendDishUrl = protocol + domain + port + "/user/dish/recommend";
    QString searchDishUrl = protocol + domain + port + "/user/dish/search";
    QString infoCerUrl = protocol + domain + port + "/user/fridge/infoCer";
    QString coverFromServerUrl = protocol + domain + port + "/user/fridge/coverFromServer";
    QString synchronizeUrl = protocol + domain + port + "/user/fridge/synchronize";
    QString downloadCoreDataUrl = protocol + domain + port + "/user/fridge/downloadCoreData";
    QString isDirtyUrl = protocol + domain + port + "/user/fridge/isDirty";
    QString coverByTerminalUrl = protocol + domain + port + "/user/fridge/coverByTerminal";
    QString bindUrl = protocol + domain + port + "/user/fridge/bindByCer";
    QString cerPath = "cer.txt";
    QString token;
    Fridge fridge;

    QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
signals:
    void loginByPasswordSuccess(QString token);
    void getUserInfoSuccess(int id,QString username,QString phone,QString email,QString avator,int gender);
    void getRecommendDishSuccess(QList<DishRecommend>* list);
    void searchDishSuccess(QList<Dish>* dishList);
    void infoCerSuccess(Fridge fridge);
    void coverFromServerSuccess();
    void downloadCoreDataSuccess();
    void isDirtyTrue();
    void isDirtyFalse();
    void coverByTerminalSuccess();
    void tokenTimeoutSignal();
    // 错误信号
    void failSignal();
    void bindSuccess();

private slots:
    // 统一监听处理函数
    void finishedSlot(QNetworkReply* reply);
    void processLoginByPassword(QJsonObject reply);
    void processUserInfo(QJsonObject reply);
    void processGetRecommendDish(QJsonObject reply);
    void processSearchDish(QJsonObject reply);
    void processInfoCer(QJsonObject reply);
    void processCoverFromServer(QJsonObject reply);
    void processDownloadCoreData(QJsonObject reply);
    void processIsDirty(QJsonObject reply);
    void processCoverByTerminal(QJsonObject reply);
    void processBind(QJsonObject reply);
};

#endif // API_H
